/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unitn.science.matr155863_148346_145963.bccastestore.utils;

import it.unitn.science.matr155863_148346_145963.bccastestore.DBManager;
import it.unitn.science.matr155863_148346_145963.bccastestore.items.Asta;
import it.unitn.science.matr155863_148346_145963.bccastestore.items.Offer;
import it.unitn.science.matr155863_148346_145963.bccastestore.items.Product;
import it.unitn.science.matr155863_148346_145963.bccastestore.items.User;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Luciano
 */
public class OfferDetailsValidator {
    
    public  static Offer createOffer(HttpServletRequest request,Asta asta) throws Exception{
        Offer offer=new Offer();
        Float price=null;
        try{ 
            Float minPrice=asta.getLast_offer_price();
            if(minPrice!=0){
                minPrice=minPrice+asta.getMin_increment();
            }else{
                minPrice=asta.getStart_price();
            }
            price=Float.parseFloat(request.getParameter("offer_price"));
            if(price >= minPrice){               
                 offer.setMaxPrice(price);  
            }else{
                throw new Exception("price");
            }
        }catch(Exception ex){
            throw new Exception("price");
        }
        if(request.getSession().getAttribute("user")!=null){
            User user=(User) request.getSession().getAttribute("user");
            offer.setBuyer(user);
            offer.setBuyer(user.getId());
        }else{
            throw new Exception("User. You must autenticate before do offer");
        }
        
        return offer;
        
    }
    
}
